#####
#####
###     Create dot plots of estimated point
###     locations for policies and states.
###     Taken from CGM unfolding analysis of
###     1982-2005 data
###
###     NOTE: These analyses use "raw" unfolded
###     policy points and state ideal points. Therefore
###     they have to be tranformed to a scale on which
###     the zero point is the mean proportion of state
###     spending devoted to collective goods policies, and
###     the units are proportions
#####
#####


library(lattice)

###
###   Read data on policy points from
###   file "Raw policy points, with names.txt"
###

policies <- read.table(file.choose(), header = T)

policies

summary(policies$point)

###
###   Transform policy points to scale in which origin
###   is mean state allocation to collective goods and 
###   units are proportions
###

policies$point <- -1.62519  + (policies$point * 4.50)

policies$point <- policies$point - 0.6673232

summary(policies$point)

policies$policy <- reorder(policies$policy, policies$point)

###
###   The following function creates Figure 1
###   from the POLITICAL ANALYSIS article
###

dotplot(policy ~ point, data = policies,
   aspect = 1.5, 
   panel = function (x, y) {
   panel.abline(h = as.numeric(y), lty = 2, col = "gray")
   panel.xyplot(x, as.numeric(y), col = "black", lty = 2,
     cex = 1.25, pch = 16)
   },
   xlab = "Policy point location",
   xlim = c(-5, 5)
)


###
###   Read 2 versions of states dataset, one with abbrevs, one with names.
###   The former are contained in the file, "State summary data, with names.txt".
###   The latter are contained in the file, "State summary data 2, with names.txt".
###
###   These files contain summary statistics (mean, minimum,
###   maximum, and standard deviation) for state policy priority
###   points across the 1982-2005 period. They are "raw" unfolded
###   scores, so the first step is to transform them so the mean
###   is zero and the units are proportions
###   

states <- read.table(file.choose(), header = T)

states2 <- read.table(file.choose(), header = T)


states$mean <- -1.62519  + (states$mean * 4.50)

states$mean.value <- mean(states$mean)

states$mean <- states$mean - states$mean.value

summary(states$mean)

summary(states$min)

states$min <- -1.62519  + (states$min * 4.50)

summary(states$min)

states$min <- states$min - states$mean.value

summary(states$min)

summary(states$max)

states$max <- -1.62519  + (states$max * 4.50)

summary(states$max)

states$max <- states$max - states$mean.value

summary(states$max)


states2$mean <- -1.62519  + (states2$mean * 4.50)

states2$mean.value <- mean(states2$mean)

states2$mean <- states2$mean - states2$mean.value

summary(states2$mean)

summary(states2$min)

states2$min <- -1.62519  + (states2$min * 4.50)

summary(states2$min)

states2$min <- states2$min - states2$mean.value

summary(states2$min)

summary(states2$max)

states2$max <- -1.62519  + (states2$max * 4.50)

summary(states2$max)

states2$max <- states2$max - states2$mean.value

summary(states2$max)

states$state <- reorder(states$state, states$mean)

summary(states$min)

summary(states$max)

###
###   Dotplot of state policy priority scores, showing
###   state abbreviations
###

dotplot(state ~ mean, data = states,
   aspect = 1.5,
   scales = list(
     y = list(cex = .55)),
   panel = function(x, y) {
   panel.dotplot(x, y, col = "black", lty = 2)
   panel.segments(states$min, as.numeric(y),
     states$max, as.numeric(y), lty = 1, col = "black")
   },
   xlab = "Mean state point locations",
   xlim = c(-.25, .25)
)

states2$state <- reorder(states2$state, states2$mean)

###
###   The following function creates Figure 2 from
###   the POLITICAL ANALYSIS article
###

dotplot(state ~ mean, data = states2,
   aspect = 1.5,
   scales = list(
     y = list(cex = .55)),
   panel = function(x, y) {
   panel.dotplot(x, y, col = "black", lty = 2)
   panel.segments(states2$min, as.numeric(y),
     states2$max, as.numeric(y), lty = 1, col = "black")
   },
   xlab = "Mean state point locations",
   xlim = c(-.25, .25)
)


